package com.example.activemq.controller;

import com.example.activemq.config.QueueConfig;
import com.example.activemq.config.TopicConfig;
import com.example.activemq.pojo.MessagePojo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/4/2 23:12
 * @Description TODO
 */
@RestController
@Slf4j
@RequestMapping(value = "/send")
public class ProductorController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * @Description:   注入制定的队列 {@link QueueConfig#delayQueue()}
     * @Author: LiYuhang
     * @return
     */
    @Resource(name = "delayQueue")
    private Queue delayQueue;

    //发送延时消息
    @PostMapping("/queue/delay-queue")
    public String sendDelayQueue(@RequestBody String str) {
        Map<String, Object> headers = new HashMap<>();
        // 延迟5秒
        headers.put(ScheduledMessage.AMQ_SCHEDULED_DELAY, 5000);
        log.info("延迟发送-开始");
        jmsMessagingTemplate.convertAndSend(delayQueue, str, headers);
        log.info("延迟发送-结束");
        return "success";
    }


}
